package com.game.gameObject
{
	import com.game.gameHandle.*;
	
	import flash.display.*;
	import flash.geom.*;
	
	public class AnimatedGameObject extends GameObject
	{
		protected var frameTime:Number = 0;
		protected var currentFrame:int = 0;
		protected var frameWidth:int = 32;
		protected var playOnce:Boolean = false;
		protected var aniY:Number = 0;
		
		public function AnimatedGameObject()
		{
			super();
		}
		
		public function startupAnimatedGameObject(graphics:GraphicsResource, position:Point, z:int = 0, playOnce:Boolean = false):void
		{			
			this.playOnce = playOnce;	
			//每一帧的图片宽度。		
			this.frameWidth = graphics.bitmap.width / graphics.frames;
			
			startupGameObject(graphics, position, z);
		}
		
		override public function enterFrame(dt:Number):void
		{
			if (inuse)
			{
				frameTime += dt;
				if (graphics.fps != -1)
				{
					while (frameTime > 1/graphics.fps)
					{
						frameTime -= 1/graphics.fps;
						currentFrame = (currentFrame + 1) % graphics.frames;
						if (currentFrame == 0 && playOnce)
						{
							shutdown();
							break;
						}
					}
				}
			}
		}
		
		override public function copyToBackBuffer(db:BitmapData):void
		{
			if (inuse)
			{
				var drawRect:Rectangle = new Rectangle(currentFrame * frameWidth, this.aniY, frameWidth, 32);
				db.copyPixels(graphics.bitmap, drawRect, position, graphics.bitmapAlpha, new Point(drawRect.x, this.aniY), true);
			}
		}
		
	}
}